﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using Thor.Common;
using Thor.Domain;


namespace Thor.Domain
{
    public class InvalidLogon : ThorDomainObjectBase, IInvalidLogon
    {
        #region Constants

        private const string USERNAME = "UserName";
        private const string ATTEMPTCOUNT = "AttemptCount";
        private const string FIRSTATTEMPTTIMESTAMP = "FirstAttemptTimestamp";
        private const string LASTATTEMPTTIMESTAMP = "LastAttemptTimestamp";
        private const string ID = "Id";

        #endregion

        #region Fields

        private string _userName = string.Empty;
        private int _attemptCount = 0;
        private DateTime _firstAttemptTimestamp;
        private DateTime _lastAttemptTimestamp;
        private Guid _id = Guid.Empty;
        private bool _isEncrypted = false;
        private InvalidLogon _cache = null;

        #endregion

        #region Constructors

        public InvalidLogon() : base() { }

        public InvalidLogon(bool isEncrypted)
            : base()
        {
            _isEncrypted = isEncrypted;
        }

        #endregion

        #region Properties

        [Key]
        public Guid Id
        {
            get { return _id; }
            set
            {
                if (_id != value)
                {
                    _id = value;
                    OnPropertyChanged(ID);
                }
            }
        }

        [Required]
        public string UserName
        {
            get { return _userName; }
            set
            {
                if (_userName != value)
                {
                    _userName = value;
                    OnPropertyChanged(USERNAME);
                }
            }
        }

        public int AttemptCount
        {
            get { return _attemptCount; }
            set
            {
                if (_attemptCount != value)
                {
                    _attemptCount = value;
                    OnPropertyChanged(ATTEMPTCOUNT);
                }
            }
        }

        public DateTime FirstAttemptTimestamp
        {
            get { return _firstAttemptTimestamp; }
            set
            {
                if (_firstAttemptTimestamp != value)
                {
                    _firstAttemptTimestamp = value;
                    OnPropertyChanged(FIRSTATTEMPTTIMESTAMP);
                }
            }
        }

        public DateTime LastAttemptTimestamp
        {
            get { return _lastAttemptTimestamp; }
            set
            {
                if (_lastAttemptTimestamp != value)
                {
                    _lastAttemptTimestamp = value;
                    OnPropertyChanged(LASTATTEMPTTIMESTAMP);
                }
            }
        }

        #endregion

        #region Methods

        public override void BeginEdit()
        {
            _cache = new InvalidLogon();
            _cache.UserName = this.UserName;
            _cache.AttemptCount = this.AttemptCount;
            _cache.FirstAttemptTimestamp = this.FirstAttemptTimestamp;
            _cache.LastAttemptTimestamp = this.LastAttemptTimestamp;
            _cache.Id = this.Id;
        }

        public override void CancelEdit()
        {
            this.AttemptCount = _cache.AttemptCount;
            this.FirstAttemptTimestamp = _cache.FirstAttemptTimestamp;
            this.LastAttemptTimestamp = _cache.LastAttemptTimestamp;
            this.UserName = _cache.UserName;
            this.Id = _cache.Id;
        }

        public override void EndEdit()
        {
            _cache = null;
        }

        #endregion
    }
}
